package com.freeman.cement.controller;
//import java.io.File;
//import com.freeman.cement.util.TxtUtil;
import com.freeman.cement.entity.ResponseResult;
import com.freeman.cement.exception.AppException;
import com.freeman.cement.system.BllFrame;
import com.freeman.cement.util.TxtUtil;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;


/**
 *  查询系统帮助、版本日期
 *
 */
@RestController
@RequestMapping("/sysinfo")
@CrossOrigin    //允许跨域访问
public class SysInfoController {

    @RequestMapping(value = "/getInfo")
    public ResponseResult getInfo() {
        try {
            //File cfgFile = ResourceUtils.getFile("classpath:版本说明.txt");
            //info = TxtUtil.txt2String(cfgFile);
            //return info;

            InputStream in = ResourceUtils.class.getClassLoader().getResourceAsStream("config/版本说明.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(in, "utf8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuffer sb = new StringBuffer();
            String text = null;
            while ((text = bufferedReader.readLine()) != null) {
                sb.append(text + ";");
            }
            return ResponseResult.successMsg(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(50050, "读取版本信息错误：" + e.getMessage());
        }
    }

    //查询版本信息，支持post和get访问
    /*@RequestMapping(value = "/getInfo", produces = "text/plain;charset=utf-8")
    public String version() {
        String info = "";
        try {
            //File cfgFile = ResourceUtils.getFile("classpath:版本说明.txt");
            //info = TxtUtil.txt2String(cfgFile);
            //return info;

            InputStream in = ResourceUtils.class.getClassLoader().getResourceAsStream("config/版本说明.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(in);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuffer sb = new StringBuffer();
            String text = null;
            while ((text = bufferedReader.readLine()) != null) {
                sb.append(text + "\r\n");
            }
            return info = sb.toString();
        } catch (Exception e) {
            info = e.getMessage();
            e.printStackTrace();
        }
        return info;
    }*/

    //心跳回复
    @RequestMapping(value = "/heart")
    public ResponseResult heart() {
        return ResponseResult.successMsg("ok");
    }

    //获取登录人员列表
    @RequestMapping(value = "/loginList")
    @ResponseBody
    public ResponseResult loginList() {
        return ResponseResult.success(BllFrame.loginUsrsCache);
    }

}
